package com.xr.core.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xr.core.system.common.annotation.OperationLogAnno;
import com.xr.core.system.common.base.MyPage;
import com.xr.core.system.common.base.Result;
import com.xr.core.system.common.enums.OperationType;
import com.xr.core.system.common.groups.Save;
import com.xr.core.system.common.groups.Update;
import com.xr.core.system.common.utils.CurrentLoginUserUtil;
import com.xr.core.system.entity.userModule.AdminMenuVO;
import com.xr.core.system.entity.userModule.AssociatedRoleDTO;
import com.xr.core.system.entity.userModule.PasswordDTO;
import com.xr.core.system.entity.userModule.User;
import com.xr.core.system.service.UserService;
import com.xr.qiniuyun.service.QiniuyunFileService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 总后台用户表(User)表控制层
 */
@Validated
@RestController
@RequestMapping("/api/v1/account")
public class UserController {

    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Resource
    private QiniuyunFileService qiniuyunFileService;

    /**
     * 上传头像
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadAvatar")
    @OperationLogAnno(module = "用户模块", type = OperationType.UPLOAD, desc = "上传用户头像")
    public com.xr.common.utils.R uploadFile(@RequestBody MultipartFile file) {
        return qiniuyunFileService.uploadFile(file);
    }

    /**
     * 修改当前登录人密码
     *
     * @param passwordDTO
     * @return
     */
    @OperationLogAnno(module = "账户表模块", type = OperationType.UPDATE, desc = "修改当前登录人密码")
    public Result<Object> setPassword(@RequestBody PasswordDTO passwordDTO) {
        return Result.isSuccess(userService.setPassword(passwordDTO));
    }

    /**
     * 用户关联角色
     *
     * @param associatedRoleDTO
     * @return
     */
    @PostMapping("/associatedRole")
    @OperationLogAnno(module = "账户表模块", type = OperationType.UPDATE, desc = "用户关联角色")
    public Result<Object> associatedRole(@RequestBody AssociatedRoleDTO associatedRoleDTO) {
        return Result.isSuccess(userService.associatedRole(associatedRoleDTO));
    }

    /**
     * 启禁用账户
     *
     * @param accountId
     * @return
     */
    @PutMapping("/disOrEnaAccount")
    @OperationLogAnno(module = "账户表模块", type = OperationType.UPDATE, desc = "启禁用账户")
    public Result<Object> disOrEnaAccount(@NotNull(message = "账号id不能为空") Long accountId) {
        return Result.isSuccess(userService.disOrEnaAccount(accountId));
    }

    /**
     * 获取当前登录人菜单
     *
     * @return
     */
    @GetMapping("/menu")
    public Result<List<AdminMenuVO>> getMenuList() {
        return Result.success(userService.getMenuList());
    }

    /**
     * 查询当前登录人信息
     *
     * @return
     */
    @GetMapping("/userInfo")
    public Result<User> userInfo() {
        return Result.success(userService.getById(CurrentLoginUserUtil.getUserId()));
    }

    /**
     * 查询所有数据
     *
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryAll")
    public Result<List<User>> queryAll(User user) {
        user.setEnable(true);
        return Result.success(userService.list(new QueryWrapper<>(user)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryByPage")
    public Result<MyPage<User>> queryByPage(MyPage<User> page, User user) {
        return Result.success(userService.page(page, new QueryWrapper<>(user)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryOne/{id}")
    public Result<User> selectOne(@PathVariable Serializable id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @OperationLogAnno(module = "账户表模块", type = OperationType.INSERT, desc = "新增数据")
    public Result<Object> insert(@Validated(Save.class) @RequestBody User user) {
        return Result.isSuccess(userService.insert(user));
    }

    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @OperationLogAnno(module = "账户表模块", type = OperationType.UPDATE, desc = "修改数据")
    public Result<Object> update(@Validated(Update.class) @RequestBody User user) {
        return Result.isSuccess(userService.update(user));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @OperationLogAnno(module = "账户表模块", type = OperationType.DELETE, desc = "删除单条数据")
    public Result<Object> delete(@PathVariable Long id) {
        User user = new User();
        LambdaUpdateWrapper<User> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(User::getId, id).eq(User::getInitial, false);
        return Result.isSuccess(userService.deleteWithFill(user, updateWrapper));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/batchDelete")
    @OperationLogAnno(module = "账户表模块", type = OperationType.DELETE, desc = "批量删除数据")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        User user = new User();
        user.setInitial(false);
        return Result.isSuccess(userService.batchDelete(idList, user));
    }

    /**
     * 批量设置内置账户
     *
     * @param idList
     * @return
     */
    @PutMapping("/setInitAccount")
    @OperationLogAnno(module = "账户表模块", type = OperationType.UPDATE, desc = "批量设置内置账户")
    public Result<Object> setInitAccount(@RequestBody List<Long> idList) {
        return Result.isSuccess(userService.setInitAccount(idList));
    }

    /**
     * 获取全部内置账户id
     *
     * @return
     */
    @GetMapping("/listInitAccount")
    public Result<List<Object>> listInitAccount() {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getInitial, true).eq(User::getEnable, true);
        queryWrapper.select(User::getId);
        return Result.success(userService.listObjs(queryWrapper));
    }

    /**
     * 获取修改当前登录人密码的验证码
     *
     * @param response
     */
    @GetMapping("/verificationCode")
    public void verification(HttpServletResponse response) {
        userService.getVerification(response);
    }

}

